<template>
  <div>
  </div>
</template>
<script>
import Cookies from "js-cookie";
export default {
  name: "SSOLogin",
  data() {
    return {
      code:null,
      count:0,
      authUrl:"http://i.njupt.edu.cn/cas/oauth2.0/authorize?response_type=code&client_id=1515908819811618818&redirect_uri=https%3a%2f%2fsysxcx.njupt.edu.cn%2fssologin",
      };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created() {
    if(Cookies.get("ssologin")){
      this.count = parseInt(Cookies.get("ssologin"));
    }
    this.count ++;
    Cookies.set("ssologin",this.count);
    this.code = this.$route.query && this.$route.query.code;
    if(this.count<4){
      if(this.code){
        this.handleLogin();
      }else{
        window.location.href=this.authUrl;
      }
    }else{
      Cookies.set("ssologin",1);
      this.msgError("单点登录验证失败");
    }
  },
  methods: {
    handleLogin() {
      this.$store
        .dispatch("Login", {code:this.code})
        .then(() => {
          this.$router.push({ path: this.redirect || "/" });
        })
        .catch(() => {
          this.loading = false;
          window.location.href=this.authUrl;
        });
    },
  },
};
</script>
